﻿using System;
using System.Data;
using System.Data.SqlClient;
using Evaluation3.utils;
using Evaluation3.interfaces ;

namespace Evaluation3.services
{
	/// <summary>
	/// TùngLA 30-05-2007
    /// Thực hiện các thao tác nghiệp vụ với đối tượng User
	/// </summary>
	public class usersServices
    {
        #region Khai báo các biến sử dụng
        string hovaten;
        string tentruycap;
        string diachi;
        string dienthoai;
        string hopthu;
        string tencongty;
        string linhvuckinhdoanh;
        string logo;
        string dNamThanhLap;
        int iMaDoanhNghiep;
        string sqlStr;
        #endregion

        #region Properties
        public string HovaTen { get { return hovaten; }  set { this.hovaten = value; } }
        public string DiaChi { get { return diachi; } set { this.diachi = value; } }
        public string DienThoai { get { return dienthoai; } set { this.dienthoai = value; } }
        public string HopThu { get { return hopthu; } set { this.hopthu = value; } }
        public string TenCongTy { get { return tencongty; } set { this.tencongty = value; } }
        public string LinhVucKinhDoanh { get { return linhvuckinhdoanh; } set { this.linhvuckinhdoanh = value; } }
        public string TenTruyCap { get { return tentruycap; } set { this.tentruycap = value; } }
        public string Logo { get { return logo; } set { logo = value; } }
        public string NamThanhLap { get { return dNamThanhLap; } set { dNamThanhLap = value; } }
        public int MaDoanhNghiep { get { return iMaDoanhNghiep; } set { iMaDoanhNghiep = value; } }

        SqlParameter[] parr;
        #endregion

        #region Constructor
        public usersServices()
		{
            hovaten = string.Empty;
            tentruycap = string.Empty;
            diachi = string.Empty;
            dienthoai = string.Empty;
            hopthu = string.Empty;
            tencongty = string.Empty;
            linhvuckinhdoanh = string.Empty;
            logo = string.Empty;
            dNamThanhLap = string.Empty;
            iMaDoanhNghiep = 0;

            sqlStr = string.Empty;
        }
        #endregion

        #region static void User_RegNew()
        public void User_RegNew()
        {
            sqlStr = "User_RegNew";
            parr = new SqlParameter[9];
            parr[0] = new SqlParameter("@Hovaten", SqlDbType.NVarChar, 100);
            parr[0].Value = HovaTen;
            parr[1] = new SqlParameter("@Tentruycap", SqlDbType.NVarChar, 48);
            parr[1].Value = TenTruyCap;
            parr[2] = new SqlParameter("@Diachi", SqlDbType.NVarChar, 100);
            parr[2].Value = DiaChi;
            parr[3] = new SqlParameter("@Dienthoai", SqlDbType.NVarChar, 20);
            parr[3].Value = DienThoai;
            parr[4] = new SqlParameter("@Hopthu", SqlDbType.NVarChar, 100);
            parr[4].Value = HopThu;
            parr[5] = new SqlParameter("@Tencongty", SqlDbType.NText);
            parr[5].Value = TenCongTy;
            parr[6] = new SqlParameter("@Linhvuckinhdoanh", SqlDbType.NText);
            parr[6].Value = LinhVucKinhDoanh;
            parr[7] = new SqlParameter("@logo", SqlDbType.Text);
            parr[7].Value = Logo;
            parr[8] = new SqlParameter("@Namthanhlap", SqlDbType.SmallDateTime);
            parr[8].Value = NamThanhLap;
            dataUtils.ExecuteNonQuery(sqlStr, CommandType.StoredProcedure, parr);
        }
        #endregion

        #region User_GetCompanyInfo()
        /// <summary>
        /// Lấy ra thông tin về Doanh nghiệp
        /// </summary>
        /// <returns>DataView</returns>
        public DataTable User_GetCompanyInfo()
        {
            sqlStr = "SELECT * FROM DoanhNghiep WHERE MaDoanhNghiep = @MaDoanhNghiep";
            parr = new SqlParameter[1];
            parr[0] = new SqlParameter("@MaDoanhNghiep", SqlDbType.Int);
            parr[0].Value = MaDoanhNghiep;
            return (DataTable)dataUtils.ExecuteQuery(sqlStr, CommandType.Text, parr).Tables[0];
        } 
        #endregion

        #region KiemTraEmail
        /// <summary>
        /// Kiểm tra xem email đã tồn tại trong hệ thống chưa
        /// </summary>
        /// <returns>false nếu đã tồn tại nên không hợp lệ để đăng ký</returns>
        public bool IsValidEmail()
        {
            sqlStr = "SELECT * FROM guest WHERE HopThu=@Email";
            parr = new SqlParameter[1];
            parr[0] = new SqlParameter("@Email", SqlDbType.VarChar, 255);
            parr[0].Value = HopThu;

            DataTable dt = dataUtils.ExecuteQuery(sqlStr, CommandType.Text, parr).Tables[0];
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            else return true;
        }
        #endregion
    }
}